package com.to3ks.excelservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.to3ks.excelservice.entity.XlsSwry;
import com.to3ks.excelservice.mapper.SwryxlsSqlBatchMapper;
import com.to3ks.excelservice.mapper.XlsSwryMapper;
import com.to3ks.excelservice.service.XlsSwryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.to3ks.excelservice.vo.XlsSwryQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 税务 服务实现类
 * </p>
 *
 * @author Tigger
 * @since 2021-01-08
 */
@Transactional
@Service
public class XlsSwryServiceImpl extends ServiceImpl<XlsSwryMapper, XlsSwry> implements XlsSwryService {


    @Autowired
    private SwryxlsSqlBatchMapper swryxlsSqlBatchMapper;



    //@Cacheable(value = "index",  keyGenerator = "smpkeyGenerator", unless = "#result==null")
    @Cacheable(value = "index",  key = "'::selectPage_'+#xlsSwryQueryVo.getId()+#xlsSwryQueryVo.getSwryxm()+#xlsSwryQueryVo.getSwryDm()+#xlsSwryQueryVo.getLxdh()+#xlsSwryQueryVo.getSfzjhm()+#pageParam.current+#pageParam.size", unless = "#result==null")
    @Override
    public IPage<XlsSwry> selectPage(Page<XlsSwry> pageParam, XlsSwryQueryVo xlsSwryQueryVo) {

        //显示分页查询列表
//        1、排序：按照sort字段排序
        QueryWrapper<XlsSwry> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");

//        2、分页查询
        if(xlsSwryQueryVo == null){
            return baseMapper.selectPage(pageParam, queryWrapper);
        }

//        3、条件查询
        String swryDm = xlsSwryQueryVo.getSwryDm();
        String swryxm = xlsSwryQueryVo.getSwryxm();
        String lxdh = xlsSwryQueryVo.getLxdh();
        String sfzjhm = xlsSwryQueryVo.getSfzjhm();
        String strId =xlsSwryQueryVo.getId();

        if(!StringUtils.isEmpty(strId)){
            queryWrapper.likeRight("id", strId);
        }

        if(!StringUtils.isEmpty(swryxm)){
            queryWrapper.like("swryxm", swryxm);
        }

        if(swryDm != null){
            queryWrapper.likeRight("swry_dm", swryDm);
        }

        if(!StringUtils.isEmpty(lxdh)){
            queryWrapper.likeRight("lxdh", lxdh);
        }

        if(!StringUtils.isEmpty(sfzjhm)){
            queryWrapper.likeRight("sfzjhm", sfzjhm);
        }

        return baseMapper.selectPage(pageParam, queryWrapper);

    }

    //@Cacheable(value = "index", key = "'selectNameList'")
    @Cacheable(value = "index",  key = "'::selectNameList_'+#key", unless = "#result==null")
    @Override
    public List<Map<String, Object>> selectNameList(String key) {
        QueryWrapper<XlsSwry> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("swryxm");
        queryWrapper.likeRight("swryxm", key);

        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);
        return list;
    }

    @Override
    public void sqlBatchInsert(List list) {
        swryxlsSqlBatchMapper.sqlBatchInsert(list);
    }

}
